import { create } from 'zustand';

const mockDishes = [
  {
    id: '1',
    name: 'IPA精酿啤酒',
    slug: 'ipa-craft-beer',
    category: 'IPA',
    images: ['https://images.unsplash.com/photo-1608270586620-248524c67de9?w=400', 'https://images.unsplash.com/photo-1618885472179-5e474019f2a9?w=400'],
    variants: [
      { size: '330ml', price: 25, discount: 100 },
      { size: '500ml', price: 45, discount: 90 }
    ],
    status: 'online',
    launchTime: '2024-01-15 10:00:00',
    createdAt: '2024-01-10 09:00:00',
    updatedAt: '2024-01-15 10:00:00'
  },
  {
    id: '2',
    name: '世涛黑啤',
    slug: 'stout-beer',
    category: 'Stout',
    images: ['https://images.unsplash.com/photo-1535958636474-b021ee887b13?w=400'],
    variants: [
      { size: '330ml', price: 28, discount: 100 },
      { size: '500ml', price: 48, discount: 95 }
    ],
    status: 'online',
    launchTime: '2024-02-01 14:00:00',
    createdAt: '2024-01-25 11:00:00',
    updatedAt: '2024-02-01 14:00:00'
  },
  {
    id: '3',
    name: '小麦白啤',
    slug: 'wheat-beer',
    category: 'Wheat',
    images: ['https://images.unsplash.com/photo-1618885472179-5e474019f2a9?w=400', 'https://images.unsplash.com/photo-1608270586620-248524c67de9?w=400', 'https://images.unsplash.com/photo-1571613316887-6f8d5cbf7ef7?w=400'],
    variants: [
      { size: '330ml', price: 22, discount: 100 },
      { size: '500ml', price: 38, discount: 100 }
    ],
    status: 'offline',
    launchTime: '2024-01-20 12:00:00',
    createdAt: '2024-01-15 10:00:00',
    updatedAt: '2024-03-01 09:00:00'
  },
  {
    id: '4',
    name: '比利时修道院啤酒',
    slug: 'belgian-abbey-beer',
    category: 'Belgian',
    images: ['https://images.unsplash.com/photo-1504502350688-00f5d59bbdeb?w=400'],
    variants: [
      { size: '330ml', price: 35, discount: 100 },
      { size: '500ml', price: 60, discount: 85 }
    ],
    status: 'online',
    launchTime: '2024-02-10 15:00:00',
    createdAt: '2024-02-05 13:00:00',
    updatedAt: '2024-02-10 15:00:00'
  },
  {
    id: '5',
    name: '淡色艾尔',
    slug: 'pale-ale',
    category: 'Pale Ale',
    images: ['https://images.unsplash.com/photo-1559818454-1b46997bfe30?w=400'],
    variants: [
      { size: '330ml', price: 24, discount: 100 },
      { size: '500ml', price: 42, discount: 100 }
    ],
    status: 'online',
    launchTime: '2024-03-01 11:00:00',
    createdAt: '2024-02-20 16:00:00',
    updatedAt: '2024-03-01 11:00:00'
  },
  {
    id: '6',
    name: '果香拉格',
    slug: 'fruit-lager',
    category: 'Lager',
    images: ['https://images.unsplash.com/photo-1612528443702-f6741f70a049?w=400'],
    variants: [
      { size: '330ml', price: 20, discount: 100 },
      { size: '500ml', price: 35, discount: 100 }
    ],
    status: 'offline',
    launchTime: '2024-02-15 10:00:00',
    createdAt: '2024-02-10 09:00:00',
    updatedAt: '2024-02-28 14:00:00'
  }
];

const useDishesStore = create((set) => ({
  dishes: mockDishes,
  loading: false,
  error: null,
  filters: {
    status: 'all'
  },

  getDishes: async (filters = {}) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      let filteredDishes = [...mockDishes];
      
      if (filters.status && filters.status !== 'all') {
        filteredDishes = filteredDishes.filter(
          dish => dish.status === filters.status
        );
      }
      
      set({ 
        dishes: filteredDishes, 
        loading: false,
        filters 
      });
    } catch (error) {
      set({ error: error.message, loading: false });
    }
  },

  getDishById: (id) => {
    const { dishes } = useDishesStore.getState();
    return dishes.find(dish => dish.id === id);
  },

  createDish: async (dishData) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const slug = dishData.name
        .toLowerCase()
        .replace(/[^a-z0-9\u4e00-\u9fa5]+/g, '-')
        .replace(/^-|-$/g, '');
      
      const newDish = {
        ...dishData,
        id: Date.now().toString(),
        slug,
        status: 'offline',
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString()
      };
      
      set((state) => ({
        dishes: [...state.dishes, newDish],
        loading: false
      }));
      
      return newDish;
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  updateDish: async (id, updates) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => ({
        dishes: state.dishes.map(dish =>
          dish.id === id
            ? { ...dish, ...updates, updatedAt: new Date().toISOString() }
            : dish
        ),
        loading: false
      }));
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  deleteDish: async (id) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => ({
        dishes: state.dishes.filter(dish => dish.id !== id),
        loading: false
      }));
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  toggleDishStatus: async (id, status) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => ({
        dishes: state.dishes.map(dish =>
          dish.id === id
            ? { ...dish, status, updatedAt: new Date().toISOString() }
            : dish
        ),
        loading: false
      }));
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  setFilters: (filters) => {
    set({ filters });
  },

  clearError: () => set({ error: null })
}));

export default useDishesStore;